package com.xckj.cycling.utils

import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.xckj.cycling.framework.lazyGlobalViewModel
import com.mapbox.geojson.Point
import com.mapbox.navigation.base.formatter.DistanceFormatterOptions
import com.mapbox.navigation.base.formatter.UnitType
import com.mapbox.navigation.core.formatter.MapboxDistanceFormatter
import com.xckj.cycling.R
import com.xckj.cycling.context.AppContext
import com.xckj.cycling.viewmodel.MapViewModel
import java.math.BigDecimal
import java.math.RoundingMode

var YINGLI = 0.621371192237
fun Double?.toReadable(): String {
    if (this == null) {
        return "0.00"
    }
    return BigDecimal(this * YINGLI).setScale(1, RoundingMode.HALF_DOWN).toString()
}

fun Double?.toMile(): Double {
    return (this?.times(YINGLI)) ?: 0.0
}

fun String?.toDistance(): String {
    if (this == null) {
        return "0km"
    }
    val distanceFormatter = MapboxDistanceFormatter(
        DistanceFormatterOptions.Builder(AppContext.globalContext()).unitType(UnitType.IMPERIAL)
            .build()
    )
    return distanceFormatter.formatDistance(toDouble()).toString()
}

fun Double?.meterToKm(): Double {
    if (this == null) {
        return 0.0
    }
    return (this / 1000.0 * YINGLI).toBigDecimal().setScale(3, RoundingMode.DOWN).toDouble()
}

fun Long.toFormattedTime(): String {
    return formatTime(this)
}

private fun formatTime(long: Long): String {
    val t = long / 1000
    val minute = t / 60
    val hour = minute / 60
    val second = t % 60
    return format(hour) + ":" + format(minute) + ":" + format(second)
}

private fun format(long: Long): String {
    if (long < 10) {
        return "0" + long
    }
    return long.toString()
}

fun Int.toGender(): String {
    return when (this) {
        0 -> {
            AppContext.globalContext().getString(R.string.gender_famale)
        }

        1 -> {
            AppContext.globalContext().getString(R.string.gender_male)
        }

        else -> {
            AppContext.globalContext().getString(R.string.gender_other)
        }
    }
}


private val mapViewModel: MapViewModel by lazyGlobalViewModel()
fun HashMap<String, Any>.addLocation() {
    mapViewModel.lastRecordPoint?.let {
        put("lat", it.latitude())
        put("lng", it.longitude())
    }
}

fun Point.distance(otherPoint: Point): Double {
    val lat1: Double = latitude()
    val lng1: Double = longitude()
    val lat2: Double = otherPoint.latitude()
    val lng2: Double = otherPoint.longitude()
    val patm = 2.0
    val radLat1 = rad(lat1)
    val radLat2 = rad(lat2)
    val difference = radLat1 - radLat2
    val mdifference = rad(lng1) - rad(lng2)
    var distance = patm * Math.asin(
        Math.sqrt(
            Math.pow(Math.sin(difference / patm), patm)
                    + (Math.cos(radLat1) * Math.cos(radLat2)
                    * Math.pow(Math.sin(mdifference / patm), patm))
        )
    )
    distance *= EARTH_RADIUS
    return BigDecimal(distance * YINGLI).setScale(3, RoundingMode.DOWN).toDouble()
}

private const val EARTH_RADIUS = 6378.137

private fun rad(d: Double): Double {
    return d * Math.PI / 180.0
}


fun SwipeRefreshLayout.setColor() {
    setColorSchemeResources(R.color.teal_200)
}